package com.ck567.netty.chatroom.message

import java.io.Serializable

abstract class Message : Serializable {
    /**
     * 根据消息类型字节，获得对应的消息 class
     * @param messageType 消息类型字节
     * @return 消息 class
     */
    companion object {
        fun getMessageClass(messageType: Int): Class<out Message?> {
            return messageClasses[messageType]!!
        }

        private val messageType = 0

        const val Login_RequestMessage = 0
        const val Login_ResponseMessage = 1
        const val Chat_RequestMessage = 2
        const val Chat_ResponseMessage = 3
        const val Group_CreateRequestMessage = 4
        const val Group_CreateResponseMessage = 5
        const val Group_JoinRequestMessage = 6
        const val Group_JoinResponseMessage = 7
        const val Group_QuitRequestMessage = 8
        const val Group_QuitResponseMessage = 9
        const val Group_ChatRequestMessage = 10
        const val Group_ChatResponseMessage = 11
        const val Group_MembersRequestMessage = 12
        const val Group_MembersResponseMessage = 13
        const val Ping_Message = 14
        const val Pong_Message = 15

        /**
         * 请求类型 byte 值
         */
        val RPC_MESSAGE_TYPE_REQUEST = 101

        /**
         * 响应类型 byte 值
         */
        val RPC_MESSAGE_TYPE_RESPONSE = 102

        private val messageClasses: MutableMap<Int, Class<out Message?>> = mutableMapOf<Int, Class<out Message?>>()

        init {
            messageClasses[Login_RequestMessage] = LoginRequestMessage::class.java
            messageClasses[Login_ResponseMessage] = LoginResponseMessage::class.java
            messageClasses[Chat_RequestMessage] = ChatRequestMessage::class.java
            messageClasses[Chat_ResponseMessage] = ChatResponseMessage::class.java
            messageClasses[Group_CreateRequestMessage] = GroupCreateRequestMessage::class.java
            messageClasses[Group_CreateResponseMessage] = GroupCreateResponseMessage::class.java
            messageClasses[Group_ChatRequestMessage] = GroupChatRequestMessage::class.java
            messageClasses[Group_ChatResponseMessage] = GroupChatResponseMessage::class.java
            messageClasses[Group_MembersRequestMessage] = GroupMembersRequestMessage::class.java
            messageClasses[Group_MembersResponseMessage] = GroupMembersResponseMessage::class.java
            messageClasses[Group_JoinRequestMessage] = GroupJoinRequestMessage::class.java
            messageClasses[Group_JoinResponseMessage] = GroupJoinResponseMessage::class.java
            messageClasses[Group_QuitRequestMessage] = GroupQuitRequestMessage::class.java
            messageClasses[Group_QuitResponseMessage] = GroupQuitResponseMessage::class.java
            messageClasses[Ping_Message] = PingMessage::class.java
            messageClasses[Pong_Message] = PongMessage::class.java
        }
    }

    val sequenceId = 0
    abstract fun getMessageType(): Int

    override fun toString(): String {
        return "Message(sequenceId=$sequenceId)"
    }

}